import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import MainLayout from '@/layouts/MainLayout.vue'
import InventoryView from '@/views/InventoryView.vue'
import ProcurementView from '../views/ProcurementView.vue'
import SalesView from '../views/SalesAndCustomerManagement.vue'
import FinanceView from '@/views/FinancialReportView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
   
    
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/main',
      component: MainLayout,
      redirect: (to) => {
        // 从本地存储中获取用户信息
        const user = JSON.parse(localStorage.getItem('user') || '{}');
        const roleId = user.roleId || 'guest';
        
        switch(parseInt(roleId)) {
          case 5: return { path: '/main/inventory' };
          case 7: return { path: '/main/procurement' };
          case 9: return { path: '/main/sales' };
          case 11: return { path: '/main/finance' };
          default: return { path: '/login' };
        }
      },
      children: [
        {
          path: 'inventory',
          name: 'inventory',
          component: InventoryView,
          meta: { requiresAuth: true, requiredRole: 5 }
        },
        {
          path: 'procurement',
          name: 'procurement',
          component: ProcurementView,
          meta: { requiresAuth: true, requiredRole: 7 }
        },
        {
          path: 'sales',
          name: 'sales',
          component: SalesView,
          meta: { requiresAuth: true, requiredRole: 9 }
        },
        {
          path: 'finance',
          name: 'finance',
          component: FinanceView,
          meta: { requiresAuth: true, requiredRole: 11 }
        }
      ]
    },
      {
        path: '/purchase-order',
        name: 'PurchaseOrderList',
        component: () => import('@/views/PurchaseOrder/List.vue'),
        meta: { requiresAuth: true, requiredRole: 7 }
      },
      {
        path: '/purchase-order/create',
        name: 'PurchaseOrderCreate',
        component: () => import('@/views/PurchaseOrder/Create.vue'),
        meta: { requiresAuth: true, requiredRole: 7 }
      },
      {
        path: '/purchase-order/:id',
        name: 'PurchaseOrderDetail',
        component: () => import('@/views/PurchaseOrder/Detail.vue'),
        props: true,
        meta: { requiresAuth: true, requiredRole: 7 }
      },
      // 供应商路由
      {
        path: '/supplier',
        name: 'SupplierList',
        component: () => import('@/views/Supplier/List.vue'),
        meta: { requiresAuth: true, requiredRole: 7 }
      },
      {
        path: '/supplier/create',
        name: 'SupplierCreate',
        component: () => import('@/views/Supplier/Create.vue'),
        meta: { requiresAuth: true, requiredRole: 7 }
      },
      {
        path: '/supplier/:id',
        name: 'SupplierDetail',
        component: () => import('@/views/Supplier/Detail.vue'),
        props: true,
        meta: { requiresAuth: true, requiredRole: 7 }
      }
  ]
});



export default router